package com.itmuch.youran.security.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.itmuch.youran.domain.member.entity.Member;
import com.itmuch.youran.security.SecurityUserFactory;
import com.itmuch.youran.service.member.MemberService;

@Component
public class CustomUserDetailsService implements UserDetailsService {
	@Autowired
	private MemberService memberService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// 正常情况登录
		Member member = this.memberService.findByKeys(username, username);
		if (member == null) {
			throw new UsernameNotFoundException("用户不存在");
		}

		return SecurityUserFactory.create(member);
	}
}